package com.clkj.cldisabledmanclient.mvp.login;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.UserLoginInfo;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.mvp.main.ActivityMain;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

/**
 * Created by clboys on 2019/5/15.
 */

public class ActivityLogin extends BaseActivity implements LoginContract.View {


    private EditText etUsername;
    private EditText etPassword;
    private TextView tvActionSubmitLogin;

    private LoginContract.Presenter mPresenter;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initData();
        assignView();
        initView();
        mPresenter = new LoginPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

    }

    @Override
    public void initData() {
        String loginTag = (String) CommonUtil.getShareValue(ShareKey.LOGIN_TAG, "false");
        if ("true".equals(loginTag)) {
            startActivity(new Intent(ActivityLogin.this, ActivityMain.class));
            finish();
        }
    }

    @Override
    public void assignView() {

        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        tvActionSubmitLogin = (TextView) findViewById(R.id.tv_action_submit_login);

    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {

        RxView.clicks(tvActionSubmitLogin)
                .throttleFirst(1000, TimeUnit.MILLISECONDS)
                .filter(new Predicate<Object>() {
                    @Override
                    public boolean test(Object o) throws Exception {

                        if (CommonUtil.isEditTextEmpty(etUsername, "请输入用户名") == false
                                && CommonUtil.isEditTextEmpty(etPassword, "请输入密码") == false) {
                            return true;
                        }

                        return false;
                    }
                })
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {

                        login();

                    }
                });


    }

    @Override
    public void login() {

        mPresenter.login(etUsername.getText().toString(), etPassword.getText().toString(), String.valueOf(Config.ROLEID_OF_DISABLED_MAN));

    }

    @Override
    public void onLoginSuccess(UserLoginInfo userLoginInfo) {


        CommonUtil.putShareValue(ShareKey.USER_IDCARD, userLoginInfo.getIdCard());
        CommonUtil.putShareValue(ShareKey.USER_NAME, userLoginInfo.getLoginName());
        CommonUtil.putShareValue(ShareKey.LOGIN_TAG, "true");

        Intent toMain = new Intent(this, ActivityMain.class);
        startActivity(toMain);
        finish();
    }

    @Override
    public void onLoginError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            //设置点击进度对话框外的区域对话框不消失
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMessage("正在加载...");
            progressDialog.show();
        }
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}
